<?php
/**
 * Project     Newsletter for Joomla 1.5.x
 * Release     2.0.2
 * @package    Communicator
 * @subpackage Component
 * @version    $Id$ - admin.communicator.php
 * @link       http://joomlacode.org/gf/project/communicator/
 * @copyright  Copyright (c) 2008 Stefan Granholm, Copyright 2009 Erik Damke - All rights reserved
 * @license    http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php for details
 *
 * @Author     Stefan Granholm (http://www.granholmcms.com), Erik Damke (commqc20@jocoflex.com)
 */

// Ensure this file is included via Joomla Framework
defined('_JEXEC') or die('Restricted access');

// load Version Class for this Communicator instance
require_once( JPATH_COMPONENT_ADMINISTRATOR.DS.'version.communicator.php');

require_once( $mainframe->getPath( 'admin_html' ) );
require_once( $mainframe->getPath( 'class' ) );

$document =& JFactory::getDocument();
$document->addStyleSheet('components/com_communicator/assets/toolbar.css');
$document->addStyleSheet('components/com_communicator/assets/communicator.css');

// Load configuration in constructor
$database =& JFactory::getDBO();
$communicator = new josCommunicator($database);

$cid     = JRequest::getVar('cid', array(0));
$task    = JRequest::getVar('task');
$no_html = JRequest::getVar('no_html');
$option  = JRequest::getVar( 'option', '?' );
if ($option == '?')     // undefined ?
   {
   $option = 'com_communicator';
   JRequest::setVar( 'option', $option );
   }

if (!is_array( $cid )) {
   $cid = array(0);
}
/***
if(!$no_html) {
   HTML_communicator::header();
} **/
switch( $task ) {
   case 'new':
      editNewsletter( 0 );
      break;
   case 'edit':
      editNewsletter( $cid[0] );
      break;
   case 'compose':
      HTML_communicator::composeNewsletter();
      break;
   case 'composeNow':
      composeNow();
      break;
   case 'save':
      saveNewsletter( );
      break;
   case 'remove':
      removeNewsletter( $cid );
      break;
   case 'publish':
      publishNewsletter( $cid, 1 );
      break;
   case 'unpublish':
      publishNewsletter( $cid, 0 );
      break;
   case 'cancel':
      cancelNewsletter( );
      break;
   case 'sendNow':
      cm_sendNewsletter( $cid[0] );
      break;
   case 'sendMail':
      cm_sendMail();
      break;

   //subscriber management
   case 'subscribers':
      listSubscribers();
      break;
   case 'editSubscriber':
      HTML_communicator::editSubscriber( $cid[0] );
      break;
   case 'saveSubscriber':
      saveSubscriber();
      break;
   case 'assignUsers':
      assignUsers();
      break;
   case 'deleteSubscriber':
      deleteSubscribers();
      break;
   case 'importSubscribers':
      importSubscribers();
      break;
   case 'exportSubscribers':
      doExport();
      break;
   case 'fixSubscribers':
      syncSubscribersToUsers();
      break;
   case 'validateEmails':
      validateEmailAddresses( $cid );
      break;

   // Configuration
   case 'config':
      showConfig( );
      break;
   case 'saveconfig':
      saveSettings( );
      break;
   case 'cancelconfig':
      cancelSettings( );
      break;

   default:
      viewNewsletter( );
      break;
}
HTML_communicator::footer();

function viewNewsletter( )
{
   global $mainframe;

   $database=& JFactory::getDBO();
   $option  = JRequest::getVar( 'option', 'com_communicator' );

   $limit   = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
   $limitstart = $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0, 'int' );
   $search  = $mainframe->getUserStateFromRequest( $option.'.search','search','','string');
   $search  = JString::strtolower( $search );

   $where = '';
   if ($search) {
      $where = " WHERE a.subject LIKE '%$search%' OR a.message LIKE '%$search%'";
   }

   $database->setQuery( 'SELECT COUNT(*) FROM #__communicator AS a '.$where );
   $total = $database->loadResult();

   // jed90416 require_once( 'includes/pageNavigation.php' );
   // jed90416 $pageNav = new mosPageNav( $total, $limitstart, $limit  );
   jimport('joomla.html.pagination');
   $pageNav = new JPagination( $total, $limitstart, $limit );

   $sql = 'SELECT a.*, u.name AS editor, g.name AS groupname'.
         ' FROM #__communicator AS a'.
          ' LEFT JOIN #__users AS u ON u.id=a.checked_out'.
         ' LEFT JOIN #__groups AS g ON g.id = a.access'.
         " $where ORDER BY created DESC LIMIT $pageNav->limitstart,$pageNav->limit";
   $database->setQuery( $sql );
   $rows = $database->loadObjectList();

   // search filter
   $lists['search']= $search;

   HTML_communicator::showNewsletters( $rows, $pageNav, $lists );
}

function composeNow()
{
   $nl_content = JRequest::getVar('nl_content' );
   require_once( JPATH_SITE.'/administrator/components/com_communicator/includes/contentRenderer.class.php' );
   $renderer = new cm_contentRenderer();

   $content = $renderer->getContent( $nl_content );

   HTML_communicator::composeNewsletter( $content );
}

function editNewsletter( $uid )
{
   $database  =& JFactory::getDBO();
   $user      = &JFactory::getUser();
   $row       = new josCommunicator( $database );
   // load the row from the db table
   $row->load( $uid );

   if ($uid) {
      $row->checkout( $user->id );
   } else {
      // initialise new record
      $row->published = 0;
   }

   // make the select list for the image positions
   $yesno[] = JHTML::_( 'select.option', '0', 'No' );
   $yesno[] = JHTML::_( 'select.option', '1', 'Yes' );

   // build the html select list
   $publist = JHTMLSelect::genericlist( $yesno, 'published', 'class="inputbox" size="2"',
                                 'value', 'text', $row->published );

   // get list of groups
   $database->setQuery( "SELECT id AS value, name AS text FROM #__groups ORDER BY id" );
   $groups = $database->loadObjectList();   if (!($orders = $database->loadObjectList())) {
      echo $database->stderr();
      return false;
   }

   // build the html select list
   $glist = JHTMLSelect::genericlist( $groups, 'access', 'class="inputbox" size="1"',
                                 'value', 'text', intval( $row->access ) );

   HTML_communicator::editNewsletter( $row, $publist, $glist );
}

function saveNewsletter( )
{
   global $mainframe;
   $database =& JFactory::getDBO();
   $option  = JRequest::getVar( 'option' );

   $row = new josCommunicator( $database );
   if (!$row->bind( $_POST )) {
      echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }

   if (!$row->check()) {
      echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }
   if (!$row->store()) {
      echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }
   $row->checkin();

   $mainframe->redirect( 'index.php?option='.$option );
}

/**
* Publishes or Unpublishes one or more records
* @param database A database connector object
* @param array An array of unique category id numbers
* @param integer 0 if unpublishing, 1 if publishing
* @param string The current url option
*/
function publishNewsletter( $cid=null, $publish=1 )
{
   global $mainframe;

   $database=& JFactory::getDBO();
   $user    = &JFactory::getUser();
   $option  = JRequest::getVar( 'option' );

   if (!is_array( $cid ) || count( $cid ) < 1)
      {
      $msg = $publish ? JText::_('CM_SELECT_NEWSLETTER_TO_PUBLISH')
                      : JText::_('CM_SELECT_NEWSLETTER_TO_UNPUBLISH');
      echo '<script type="text/javascript">'.
             ' alert("'.$msg.'"); window.history.go(-1);</script>'.
             "\n";
      exit;
      }

   $cids = implode( ',', $cid );

   $database->setQuery( "UPDATE #__communicator SET published='$publish'"
                 . "\nWHERE id IN ($cids) AND (checked_out=0 OR (checked_out='$user->id'))"
                 );
   if (!$database->query()) {
      echo '<script type="text/javascript">'.
            ' alert("'.$database->getErrorMsg().'"); window.history.go(-1); </script>'.
            "\n";
      exit();
   }

   if (count( $cid ) == 1) {
      $row = new josCommunicator( $database );
      $row->checkin( $cid[0] );
   }
   $mainframe->redirect( 'index.php?option='.$option );
}

/**
* Deletes one or more records
* @param array An array of unique category id numbers
* @param string The current url option
*/
function removeNewsletter( $cid )
{
   global $mainframe;

   $database =& JFactory::getDBO();
   $option  = JRequest::getVar( 'option' );

   if (count( $cid )) {
      $cids = implode( ',', $cid );
      $database->setQuery( 'DELETE FROM #__communicator WHERE id IN ('.$cids.')');
      if (!$database->query()) {
         echo '<script type="text/javascript"> alert("'.$database->getErrorMsg().'"); window.history.go(-1); </script>'."\n";
      }
   }

   $mainframe->redirect( 'index.php?option='.$option );   
}

/**
* Cancels an edit operation
* @param string The current url option
*/
function cancelNewsletter( ) 
{
   global $mainframe;
   $database=& JFactory::getDBO();
   $option  = JRequest::getVar( 'option' );
   
   $row = new josCommunicator( $database );
   $row->bind( $_POST );
   $row->checkin();
   
   $mainframe->redirect( 'index.php?option='.$option );
}


/**
 * BEGIN OF THE SUBSCRIBER SECTION
 *
 */
function listSubscribers()
{
   global $mainframe;

   $database  = &JFactory::getDBO();
   $user      = &JFactory::getUser();
   $option    = JRequest::getVar( 'option');
   $orderby   = JRequest::getVar( 'orderby', 'subscriber_name');
   $sort      = JRequest::getVar( 'ordering', 'ASC');

   $limit     = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
   $limitstart= $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0, 'int' );
   $search    = $mainframe->getUserStateFromRequest( $option.'.search','search','','string');
   $search    = JString::strtolower( $search );
   $filter_state = $mainframe->getUserStateFromRequest( $option.'filter_state', 'filter_state', '', 'word' );

   $filter_where = '';
   if ( $filter_state )
      {
      if ( $filter_state == 'C' ) {
         $filter_where = 'confirmed = 1';
         }
      else if ($filter_state == 'N' ) {
         $filter_where = 'confirmed = 0';
         }
      }

   // filtered search
   $likeSearch = '%'.$search.'%';
   $q_all = 'SELECT * FROM `#__communicator_subscribers`';
   if( !empty( $search ) )
     {
     $q_all .= ' WHERE (subscriber_name LIKE '.$database->Quote($likeSearch).
                ' OR subscriber_email LIKE '.$database->Quote($likeSearch).')';
     if ($filter_where != '')
        $q_all .= ' AND '.$filter_where;
     }
   else {
      if ($filter_where != '')
        $q_all .= ' WHERE '.$filter_where;
      }
   $q_all .= ' ORDER BY '.$orderby.' '.$sort.', subscriber_id ASC';

   $database->setQuery( $q_all );
   $subscribers = $database->loadObjectList();

   $total = sizeof( $subscribers );
   jimport('joomla.html.pagination');
   $pageNav = new JPagination( $total, $limitstart, $limit );

   $q_search = $q_all. ' LIMIT '.$pageNav->limitstart.', '.$pageNav->limit;
   $database->setQuery( $q_search );
   $rows = $database->loadObjectList();

   // state filter
   $states[] = JHTML::_('select.option', '', '- '. JText::_( 'CM_CONFIRMED' ) .' -' );
   $states[] = JHTML::_('select.option',  'C', JText::_( 'CMN_YES' ));
   $states[] = JHTML::_('select.option',  'N', JText::_( 'CMN_NO' ));

   $lists['state'] = JHTML::_('select.genericlist', $states, 'filter_state', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_state );

   // search filter
   $lists['search']= $search;

   HTML_communicator::showSubscribers( $rows, $pageNav, $lists, $orderby, $sort);
}

function assignUsers()
{
   $database  =& JFactory::getDBO();

   $q = 'SELECT id, name, username, email'.
         ' FROM `#__users`'.
         ' LEFT JOIN `#__communicator_subscribers`'.
         '   ON id = user_id OR email=subscriber_email'.
         ' WHERE user_id IS NULL'.
         ' ORDER BY name ASC, username ASC'.
         ' LIMIT 0, 1000';
   $database->setQuery( $q );
   $rows = $database->loadObjectList();

   HTML_communicator::assignUsers( $rows );
}

function saveSubscriber()
{
   global $mainframe;
   $database =& JFactory::getDBO();

   $orderby = JRequest::getVar('orderby', 'subscriber_name');
   $sort    = JRequest::getVar('ordering', 'ASC');

   $selectedUsers = JRequest::getVar('selectedUsers' );

   if ( is_array( $selectedUsers )) {
      $msg = JText::_('CM_SUBSCRIBER_SAVED');
      foreach( $selectedUsers as $user_id ) {
         $q = "SELECT email, name FROM #__users WHERE id='$user_id'";
         $database->setQuery( $q );
         $user = $database->loadObject();
         $subscriber =& new josCommunicatorSubscribers($database);
         $subscriber->user_id = $user_id;
         $subscriber->subscriber_name = $user->name;
         $subscriber->subscriber_email = $user->email;
         $subscriber->confirmed = "1";
         $subscriber->subscribe_date = date( "Y:m:d h:i:s", time() );
         if( !$subscriber->store() ) {
            $msg .= JText::_("Error storing User ").$user->name;
         }
      }
      $mainframe->redirect( "index.php?option=com_communicator&task=subscribers");
   }
   elseif ( !empty( $_POST['subscriber_email'] )) {

      $subscr_id = JRequest::getInt( 'subscriber_id', 0 );
      if ($subscr_id == 0)      // new subscriber ?
         {
         // check if email already exists
         $check = 'SELECT user_id FROM #__communicator_subscribers'.
                ' WHERE subscriber_email = '.$database->Quote($_POST['subscriber_email']);

         $database->setQuery( $check );
         $result = $database->loadObject();

         if( $result ) {
            $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby",
                        JText::_('CM_ERROR_EMAIL_ALREADY_ONLIST'));
         }
      }

      $row = new josCommunicatorSubscribers( $database );
      // load the row from the db table
      if (!$row->bind( $_POST )) {
         echo '<script type="text/javascript"> alert(\''.$row->getError()."'); window.history.go(-1); </script>\n";
         exit();
      }

      $row->subscribe_date = date( "Y-m-d H:i:s" );

      if (!$row->check()) {
         $msg = JText::_('CM_ERROR_FATAL').': '.$row->getError();
         // echo '<script type="text/javascript"> alert(\''.$row->getError()."'); window.history.go(-1); </script>\n";
         // exit();
         $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", $msg);
      }

      if (!$row->store()) {
         $msg = JText::_('CM_ERROR_FATAL').': '.$row->getError();
         //echo '<script type="text/javascript"> alert(\''.$row->getError()."'); window.history.go(-1); </script>\n";
         //exit();
         $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", $msg);
      }

      if (empty($row->subscriber_id)) {
          $row->subscriber_id = $database->insertid();
      }

      $error = $database->getErrorMsg();
      if (!empty($error)) {
         //echo '<script type="text/javascript"> alert(\''.JText::_('CM_ERROR_EMAIL_ALREADY_ONLIST')."'); location.href = 'index2.php?option=com_communicator&task=subscribers'; </script>\n";
         //exit();
         $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", JText::_('CM_ERROR_EMAIL_ALREADY_ONLIST'));
      }
      else {
         $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", JText::_('CM_SUBSCRIBER_SAVED'));
      }
   }
   else {
      $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby" );
   }
}

function deleteSubscribers()
{
   global $mainframe;
   $database  =& JFactory::getDBO();

   $orderby   = JRequest::getVar('orderby', 'subscriber_name');
   $sort      = JRequest::getVar('ordering', 'ASC');

   //$cid       = implode(", ", $_REQUEST['cid']);
   $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
   // $count     = sizeof( $_REQUEST['cid']);
   $count     = sizeof( $cid );

   if (empty( $cid )) {
         return JError::raiseWarning( 500, JText::_( 'CM_SELECT_SUBSCRIBER' ) );
      }

   JArrayHelper::toInteger( $cid );
   $cids = implode( ',', $cid );

   $query     = 'DELETE FROM `#__communicator_subscribers`'.
                 ' WHERE subscriber_id IN ('.$cids.')';
   $database->setQuery($query);
   $database->query();

   $error = $database->getErrorMsg();
   if (!empty($error)) {
      $msg = JText::_('CM_ERROR_FATAL').': '.$error;
      //echo "<script type=\"text/javascript\"> alert('". $error ."'); </script>\n";
      //exit();
      $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", $msg);
   }
   $msg = '';
   if ($count > 1)
      $msg = str_replace( "{X}", $count, JText::_('CM_SUBSCRIBERS_DELETED'));
   else
      $msg = JText::_('CM_SUBSCRIBER_DELETED');
   $mainframe->redirect( "index.php?option=com_communicator&task=subscribers&ordering=$sort&orderby=$orderby", $msg);
}


function doExport()
{
   global $mainframe;

   $database =& JFactory::getDBO();

   // Workaround for GZIP = On
   /** removed JED 1.3.09 since there is not index3.php
   if( stristr( $_SERVER['PHP_SELF'], "index2" ) ) {
      $mainframe->redirect( "index3.php?option=com_communicator&task=exportSubscribers&no_html=1" );
   } */

   if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
      $UserBrowser = "Opera";
   }
   elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
      $UserBrowser = "IE";
   } else {
      $UserBrowser = '';
   }
   $mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';

   $filename = "BackupList_Communicator_from_" . date('d-m-Y');

   $output = '<?xml version="1.0" encoding="UTF-8" ?>
              <!-- Communicator export file -->
              <!DOCTYPE subscribers [
              <!ELEMENT subscribers (subscriber+)>
              <!ELEMENT subscriber (subscriber_id, name, email, confirmed, subscribe_date)>
              <!ELEMENT subscriber_id (#PCDATA)>
              <!ELEMENT name (CDATA)>
              <!ELEMENT email (#PCDATA)>
              <!ELEMENT confirmed (#PCDATA)>
              <!ELEMENT subscribe_date (#PCDATA)>
              ]>
              <subscribers>
              ';
   $q = "SELECT subscriber_id,subscriber_name,subscriber_email,confirmed,subscribe_date FROM #__communicator_subscribers ORDER BY subscriber_name ASC";
   $database->setQuery( $q );
   $subscribers = $database->loadObjectList();

   foreach ($subscribers AS $subscriber){
      $output .= "  <subscriber>\n";
      $output .= "    <subscriber_id>" . $subscriber->subscriber_id . "</subscriber_id>\n";
      $output .= "    <name><![CDATA[" . htmlentities(html_entity_decode($subscriber->subscriber_name)) . "]]></name>\n";
      $output .= "    <email>" . $subscriber->subscriber_email . "</email>\n";
      $output .= "    <confirmed>" . $subscriber->confirmed . "</confirmed>\n";
      $output .= "    <subscribe_date>" . $subscriber->subscribe_date . "</subscribe_date>\n";
      $output .= "  </subscriber>\n";
   }
   $output .= "</subscribers>";


   //send file to browser

   @ob_end_clean();
   ob_start();

   header('Content-Type: ' . $mime_type);
   header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');

   if ($UserBrowser == 'IE') {
      header('Content-Disposition: inline; filename="' . $filename . '.xml"');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
   }
   else {
      header('Content-Disposition: attachment; filename="' . $filename . '.xml"');
      header('Pragma: no-cache');
   }
   print $output;
   exit();
}

function importSubscribers()
{
   global $mainframe;

   $database  =& JFactory::getDBO();
   $option    = JRequest::getVar('option');
   //send mailing to an entered emailadres
   if(!empty($_FILES['xmlfile']) && !empty($_FILES['cvsfile'])){
      @ini_set('memory_limit', '16M');
      ?>
     <table class="adminheading">
      <tr>
         <th><?php echo JText::_('CM_IMPORT_USERS') ?></th>
      </tr>
      </table>

      <table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
      <tr>
      <th colspan="2" class="title">&nbsp;</th>
    </tr>
    <tr>
      <td>
   <?php
   if(!empty($_FILES['xmlfile']['name'])){
      if($_FILES['xmlfile']['type'] == "text/xml"){
         $filename = $_FILES['xmlfile']['name'];

         $path = JPATH_CACHE . '/';
         if( is_writable($path) ) {

            if(!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $path . $filename)) {
               print "<font class=\"error\">".JText::_('CM_UPLAOD_FAILED').": " . $_FILES['xmlfile']['error'] . "</font><br>\n";
            }
            else {
               // We could need this later, when utf-8 is fully supported
               $iso  = explode ('=', _ISO );
               $iso = strtolower( $iso[1] );
               $data = file_get_contents( $path . $filename );

               $data = str_replace( '&amp;', '&', $data );

               if( !stristr( $data, '<name><![CDATA[')) {
                  $data = str_replace( '<name>', '<name><![CDATA[', $data );
                  $data = str_replace( '</name>', ']]></name>', $data );
                  $data = str_replace( '<name><![CDATA[<![CDATA[', '<name><![CDATA[', $data );
                  $data = str_replace( ']]>]]></name>', ']]></name>', $data );
               }

               /* XML Parsing */
               require_once( JPATH_SITE. '/includes/domit/xml_domit_lite_include.php' );
               $xmlDoc =& new DOMIT_Lite_Document();
               //if( $iso == 'utf-8' ) {
               //   $res = $xmlDoc->parseXML_utf8( $data, false, true );
               //}
               //else {
                  $res = $xmlDoc->parseXML( $data, false, true );
               //}
               if( !$res ) {
                  $mainframe->redirect( 'index.php?option=com_communicator&task=importSubsribers', JText::_('CM_ERROR_PARSING_XML') );
               }

               $nodelist = $xmlDoc->getElementsByTagName( "subscriber" );

               if($nodelist->getLength() > 0){
                  $count = 0;
                  $num = $nodelist->getLength();
                  if( $num > 0 ) {
                     $q = "REPLACE INTO #__communicator_subscribers VALUES ";
                  }

                  for ($i = 0; $i < $num; $i++) {
                     $currNode =& $nodelist->item($i);
                     $subscriber['subscriber_id'] = $currNode->childNodes[0]->getText();
                     $subscriber['name'] = html_entity_decode( $currNode->childNodes[1]->getText());
                     $subscriber['email'] = $currNode->childNodes[2]->getText();
                     $subscriber['confirmed'] = $currNode->childNodes[3]->getText();
                     $subscriber['subscribe_date'] = $currNode->childNodes[4]->getText();

                     if( !empty( $subscriber['email'] )) {
                        if( empty( $subscriber['name'])) {
                           $subscriber['name'] = JText::_('CM_SUBSCRIBER');
                        }
                        $q .= "( '"
                        . $subscriber['subscriber_id'] . "', '', '"
                        . $subscriber['name'] . "', '"
                        . $subscriber['email'] . "', '"
                        . $subscriber['confirmed'] . "', '"
                        . $subscriber['subscribe_date'] . "')";
                        if( $i+1 < $num ) {
                           $q .= ", \n";
                        }
                        else {
                           $q .= "; ";
                        }
                     }

                     $count++;
                  }

                  $database->setQuery($q);
                  $database->query();
                  /*$error = $database->getErrorNum();
                  if( $error ) {
                     if($error == 1062){
                        echo '<span class="error">' . CM_ERROR_EMAIL_ALREADY_ONLIST . ": ".$subscriber['email']."'. </span><br />";
                     }
                     else{
                        echo $database->getErrorMsg() . "<br />\n";
                     }
                  }*/

                  $msg = str_replace( "{X}", $count, JText::_('CM_SUCCESS_ON_IMPORT') );
                  echo "<span class=\"message\">$msg</span><br /><br />".JText::_('CM_IMPORT_FINISHED')."<br />
               <a href=\"index.php?option=com_communicator&task=subscribers\">".JText::_('CMN_CONTINUE')."</a>";
               }

               if( !unlink($path . $filename) ) {
                  print"<font class=\"error\">" .JText::_('CM_ERROR_DELETING_FILE'). ": $path | $filename.</font><br>\n";
               }
            }
         }
         else {
            echo '<span class="error">' .JText::_('CM_DIR_NOT_WRITABLE'). '</span>';
         }
      }
      else{
         echo '<span class="error">'.JText::_('CM_ERROR_NO_XML').'</span>';
      }
   }
   else{
      if(
      ($_FILES['cvsfile']['type'] == "application/octet-stream") ||
      ($_FILES['cvsfile']['type'] == "application/vnd.ms-excel") ||
      ($_FILES['cvsfile']['type'] == "text/plain") ||
      ($_FILES['cvsfile']['type'] == "text/csv")
      ){
         $filename = $_FILES['cvsfile']['name'];

         $path = JPATH_SITE . '/cache/';
         if(is_writable($path)){

            if(!move_uploaded_file($_FILES['cvsfile']['tmp_name'], $path . $filename)){
               echo "<span class=\"error\">".JText::_('CM_UPLAOD_FAILED').": " . $_FILES['cvsfile']['error'] . "</span><br>\n";
            }
            else{
               //print_r($_FILES);
               $name_column = JRequest::getVar( 'name_column', '1');
               $address_column = JRequest::getVar( 'address_column', '2');
               $delim = JRequest::getVar( 'delimiter', ';');
               $offset = JRequest::getVar( 'record_number', 1);
               $offset = $offset - 1; //default an array starts at 0 in stead of 1
               $name_column = $name_column - 1; //default an array starts at 0 in stead of 1
               $address_column = $address_column - 1; //default an array starts at 0 in stead of 1
               $content = file($path . $filename);


               if(sizeof($content) > 0){
                  for($i = $offset; $i < sizeof($content); $i++){

                     $subscriber = explode($delim, $content[$i]);
                     // var_dump($subscriber);
                     $subscriber[$address_column] = ltrim(rtrim($subscriber[$address_column]));
                     $subscriber[$address_column] = str_replace('"', '', $subscriber[$address_column]);
                     $subscriber[$name_column] = str_replace('"', '', $subscriber[$name_column]);
                     if(!empty($subscriber[$address_column])){
                        // echo $subscriber[$address_column] . ": ". intval(check_email_address($subscriber[$address_column])) .'<br />';
                        if(check_email_address($subscriber[$address_column])){
                           $query = "REPLACE INTO #__communicator_subscribers (subscriber_name, subscriber_email, confirmed, subscribe_date)
                    VALUES('". addslashes($subscriber[$name_column]) . "', '". addslashes($subscriber[$address_column]) . "', '1', NOW());";
                           $database->setQuery($query);
                           $database->query();
                           $error = $database->getErrorNum();
                           if($error){
                              if($error == 1062){
                                 echo '<span class="error">' .JText::_('CM_ERROR_EMAIL_ALREADY_ONLIST'). ": ".$subscriber['email']."'. </span><br />";
                              }
                              else{
                                 echo $database->getErrorMsg() . "<br />\n";
                              }
                           }
                        }
                        else{
                           echo '<span class="error"><b>'.JText::_('CM_ERROR_INVALID_EMAIL').':</b> ' . $subscriber[$address_column] . '</span><br />';
                        }
                     }
                     else{
                        echo '<span class="error">'.JText::_('CM_ERROR_EMPTY_EMAIL').':' . $subscriber[$name_column] . '</span><br />';
                     }
                  }
                  echo '<br /><br />'.JText::_('CM_IMPORT_FINISHED').'<br />
               <a href="index.php?option=com_communicator&task=subscribers">'.JText::_('CMN_CONTINUE').'</a>';
               }
               else{
                  echo '<font class="error">'.JText::_('CM_ERROR_EMPTY_FILE').'</font><br />';
               }

               if(!unlink($path . $filename)){
                  print"<font class=\"error\">" .JText::_('CM_ERROR_DELETING_FILE').": ".$path ." | ". $filename . ".</font><br>\n";
               }
            }
         }
         else{
            echo '<span class="error">' .JText::_('CM_DIR_NOT_WRITABLE') . '</span><br />';
         }
      }
      else{
         echo '<span class="error">'.JText::_('CM_ERROR_ONLY_TEXT').'</span><br />';
      }
   }
   ?>
    </td>
  </tr>
  </table>
<?php
   }
   else{
?>
    <table>
      <tr>
         <th><?php echo JText::_('CM_SELECT_FILE') ?>:</th>
      </tr>
   </table>
      <div id="overDiv" style="position:absolute; visibility:hidden; z-index:10000;"></div>
      <script language="Javascript" src="<?php echo JURI::base();?>/includes/js/overlib_mini.js"></script>
    <form action="index.php" method="POST" name="adminForm" enctype="multipart/form-data">
    <input type="hidden" name="option" value="<?php echo $option; ?>" />
    <input type="hidden" name="task" value="importSubscribers" />
   <table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
      <tr>
            <th colspan="3" class="title">&nbsp;</th>
       </tr>
    <tr>
      <td width="200" colspan="2"><strong><?php echo JText::_('CM_YOUR_XML_FILE') ?>: </strong></td>
      <td><input type="file" name="xmlfile"></td>
    </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    <tr>
      <td width="200" colspan="2"><strong><?php echo JText::_('CM_YOUR_CSV_FILE') ?>: </strong></td>
      <td><input type="file" name="cvsfile"></td>
    </tr>
    <tr>
     <td width="20" valign="top"><?php echo JHTML::_('tooltip', JText::_('CM_POSITION_NAME') ); ?></td>
      <td><?php echo JText::_('CM_NAME_COL') ?>:</td>
      <td><input type="text" name="name_column" size="2" value="1" /></td>
    </tr>
    <tr>
     <td width="20" valign="top"><?php echo JHTML::_('tooltip', JText::_('CM_POSITION_EMAIL')); ?></td>
      <td><?php echo JText::_('CM_EMAIL_COL') ?>:</td>
      <td><input type="text" name="address_column" size="2" value="2" /></td>
    </tr>
    <tr>
     <td width="20" valign="top"><?php echo JHTML::_('tooltip', JText::_('CM_STARTFROM')); ?></td>
      <td><?php echo JText::_('CM_STARTFROMLINE') ?>:</td>
      <td><input type="text" name="record_number" size="2" value="1" /></td>
    </tr>
    <tr>
     <td width="20" valign="top"><?php echo JHTML::_('tooltip', JText::_('CM_CSV_DELIMITER_TIP')); ?></td>
      <td><?php echo JText::_('CM_CSV_DELIMITER') ?>:</td>
      <td><input type="text" name="delimiter" size="2" value=";"/></td>
    </tr>
    </table>
    </form>
<?php
   }
}

function syncSubscribersToUsers()
{
   global $mainframe;
   $database =& JFactory::getDBO();

   // First step: Find registered users who are subscribed, but have an empty user_id entry
   $q = 'SELECT subscriber_id, name, email, id'
        .' FROM `#__communicator_subscribers`, `#__users`'
        .' WHERE email=subscriber_email AND user_id = 0'
        .' GROUP BY subscriber_id';
   $database->setQuery( $q );
   $usersToFix = $database->loadObjectList();
   $fixed_ids = 0;
   if( $usersToFix) {
      foreach ( $usersToFix as $user) {
         $q = 'UPDATE `#__communicator_subscribers` SET user_id ='.$user->id.', subscriber_name=\''.$user->name.'\' WHERE subscriber_id='.$user->subscriber_id;
         $fixed_ids++;
      }
   }

   // Second step: Find unconfirmed accounts of registered users who have changed their email address since newsletter subscription
   $q = 'SELECT subscriber_email, email, subscriber_id'
         .' FROM `#__communicator_subscribers` , `#__users`'
         .' WHERE user_id = id'
         .' AND email != subscriber_email'
         .' AND confirmed =0';
   $database->setQuery($q);
   $usersToFix = null;
   $usersToFix = $database->loadObjectList();
   $fixed_emails = 0;
   if( $usersToFix) {
      foreach ( $usersToFix as $user) {
         $q = 'UPDATE `#__communicator_subscribers` SET subscriber_email =\''.$user->email.'\', confirmed=1 WHERE subscriber_id='.$user->subscriber_id;
         $fixed_emails++;
      }
   }

   $msg = 'Fixed '.$fixed_ids.' user_id(s) and updated '.$fixed_emails.' email address(es).';

   $mainframe->redirect( 'index2.php?option=com_communicator&task=subscribers', $msg );
}


function validateEmailAddresses( $cid )
{
   $database  =& JFactory::getDBO();
   $config    =& Jfactory::getConfig();

   require_once( JPATH_COMPONENT.'/includes/email_validation.php');
   $validator =& new email_validation_class();

   $tmp = explode( '@', $config->getValue('config.mailfrom') );
   $mailuser = $tmp[0];
   $mailserver = $tmp[1];

   $validator->timeout=5;
   $validator->data_timeout=0;
   $validator->localuser=$mailuser;
   $validator->localhost=$mailserver;
   $validator->debug=0;
   $validator->html_debug=1;
   $validator->exclude_address="";

   $results = array();

   foreach( $cid as $subscriber_id ) {
      $subscriber = new josCommunicatorSubscribers( $database );
      $subscriber->load( $subscriber_id );

      $results[$subscriber_id]['email'] = $subscriber->subscriber_email;
      $results[$subscriber_id]['name'] = $subscriber->subscriber_name;

      if( $subscriber->confirmed == 1 || strstr( $subscriber->subscriber_name, 'INVALID_') ) {
         // Skip confirmed email addresses!
         $results[$subscriber_id]['result'] = 2;
         $results[$subscriber_id]['result_txt'] = 'skipped';
         continue;
      }
      $res = $validator->ValidateEmailBox( $subscriber->subscriber_email );
      if( $res === -1) {
         $results[$subscriber_id]['result'] = -1;
         $results[$subscriber_id]['result_txt'] = 'Unable to validate the address with this host.';
      }
      elseif( $res ) {
         $results[$subscriber_id]['result'] = 1;
         $results[$subscriber_id]['result_txt'] = 'The host is able to receive email. The address could be valid.';
         $subscriber->confirmed = 1;
         $subscriber->store();
      }
      else {
         $results[$subscriber_id]['result'] = 0;
         $results[$subscriber_id]['result_txt'] = 'The host can\'t receive email or this mailbox doesn\'t exist. The address is NOT valid.';
         $subscriber->subscriber_name = 'INVALID_'.$subscriber->subscriber_name;
         $subscriber->store();
      }
   }
   HTML_communicator::showValidationResults( $results );
}


function check_email_address($email)
{
   // First, we check that there's one @ symbol, and that the lengths are right
   if (!ereg("[^@]{1,64}@[^@]{1,255}", $email)) {
      // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
      return false;
   }
   // Split it into sections to make life easier
   $email_array = explode("@", $email);
   $local_array = explode(".", $email_array[0]);
   for ($i = 0; $i < sizeof($local_array); $i++) {
      if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
         return false;
      }
   }
   if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
      $domain_array = explode(".", $email_array[1]);
      if (sizeof($domain_array) < 2) {
         return false; // Not enough parts to domain
      }
      for ($i = 0; $i < sizeof($domain_array); $i++) {
         if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
            return false;
         }
      }
   }
   return true;
}

function showConfig( )
{
   global $mainframe;
   $database=& JFactory::getDBO();

   $query = "SELECT a.id
            FROM #__components AS a
            WHERE a.name = 'Communicator'";
   $database->setQuery( $query );
   $id = $database->loadResult();

   // load the row from the db table
   $row =& JTable::getInstance('component' );
   $row->load( $id );

   // get params definitions
   $xml =& JFactory::getXMLParser('Simple');
   $params = new JParameter( $row->params, $mainframe->getPath( 'com_xml', $row->option ) );

   HTML_communicator::editConfig( $params, $id );
}

function saveSettings( )
{
   global $mainframe;
   $database=& JFactory::getDBO();
   $option  = JRequest::getVar( 'option', 'com_communicator' );
   $params  = JRequest::getVar( 'params', '' );
   
   if (is_array( $params )) {
      $txt = array();
      foreach ($params as $k=>$v) {
         $txt[] = "$k=$v";
      }
      $total = count( $txt );
      for( $i=0; $i < $total; $i++ ) {
         if ( strstr( $txt[$i], "\n" ) ) {
            $txt[$i] = str_replace( "\n", '<br />', $txt[$i] );
         }
      }
      $_POST['params'] = implode( "\n", $txt );
   }

   $id = JRequest::getVar( 'id' );
   // $row = new JTableComponent( $database );
   $row =& JTable::getInstance( 'component' );
   $row->loadByOption( 'com_communicator' );
   $row->load( $id );
   if (!$row->bind( $_POST )) {
      echo "<script type=\"text/javascript\"> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }

   if (!$row->check()) {
      echo "<script type=\"text/javascript\"> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }
   if (!$row->store()) {
      echo "<script type=\"text/javascript\"> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
      exit();
   }

   $mainframe->redirect( 'index.php?option='.$option, JText::_('CM_SETTINGS_SAVED') );
}

/**
* Cancels editing and checks in the record
*/
function cancelSettings( )
{
   global $mainframe;
   $option  = JRequest::getVar( 'option', 'com_communicator' );
   
   $mainframe->redirect( 'index.php?option='.$option );
}

function cm_getAttachments($dir)
{
   jimport( 'joomla.filesystem.folder' );
   $files = JFolder::files($dir, '.');
   $return = array();

   foreach($files AS $file)
     {
     if(is_dir($dir.DS.$file) && is_readable($dir.DS.$file))
        {
        $returnarray = cm_getAttachments($dir.DS.$file);
        foreach ($returnarray AS $tmpfile)
           {
           $return[] = $file.DS.$tmpfile;
           }
        }
     else{
          $return[] = $file;
        }
     }
   //sort($return);
   return $return;
}
/**
 * Returns a mosHTML::selectList object with all content items
 * @author Adam van Dongen
 * @return mosHTML
 */
function cm_getContentSelectList()
{
   $database =& JFactory::getDBO();

   $where = array(
      'c.state > 0',
      'c.catid=cc.id',
      );

      $items = array();
      $msg = JText::_('CM_SELECT_CONTENT_ITEM');
      $items[] = JHTML::_( 'select.option', '0', $msg );
      $query = 'SELECT s.id, s.title AS section_name'.
              ' FROM #__sections AS s'.
              ' WHERE s.scope=\'content\''.
              ' ORDER BY ordering ASC, s.title ASC'.
              ' LIMIT 0, 10';

      $database->setQuery( $query );
      $sections = $database->loadObjectList();

      foreach( $sections as $section ) {
         $items[] = JHTML::_( 'select.option', '', '- - - '.$section->section_name.' - - -' );
         $query = 'SELECT c.id, c.title, cc.title AS categorie_name'.
             ' FROM #__content AS c, #__categories AS cc'.
             ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' ).
             ' AND cc.section='.$section->id .
             ' ORDER BY c.created DESC, cc.title, c.title'.
             ' LIMIT 0, 10';

         $database->setQuery( $query );
         $rows = $database->loadObjectList();

         if (sizeof($rows) > 0){
            foreach($rows AS $row){
            $items[] = JHTML::_( 'select.option', $row->id, $row->categorie_name . " - " . $row->title );
            }
         }
      }
   $selectList = JHTMLSelect::genericlist( $items, 'contentid', 'class="inputbox" size="10" onchange="addContentTag()"', 'value', 'text', '0' );
   $selectList = str_replace( '>- - -', ' disabled="disabled">- - -', $selectList );

   return $selectList;
}